package com.team.arms.yuding.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.common.PageVO;
import com.team.arms.common.Result;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.core.page.TableDataInfo;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.common.ry.utils.poi.ExcelUtil;
import com.team.arms.yuding.domain.DoubleQualified;
import com.team.arms.yuding.domain.dto.ReservedDto;
import com.team.arms.yuding.domain.vo.ReservedVo;
import com.team.arms.yuding.mapper.ReservedMapper;
import com.team.arms.yuding.service.DoubleQualifiedService;
import com.team.arms.yuding.service.ReservedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author yjh
 * @create 2022-05-07-20:48
 * @description:
 */
@Api( tags = "已预订")
@RestController
@RequestMapping("/reservesoldiers/ReservedController")
public class ReservedController extends BaseController {

    @Autowired
    private ReservedService reservedService;
    @Autowired
    private DoubleQualifiedService doubleQualifiedService;

    @Autowired
    private ReservedMapper reservedMapper;
    /**
     * 分页查询已预订信息
     * @return
     */
    @ApiOperation("查询已预订信息")
    @GetMapping("/select")
    public AjaxResult selectPage(@ApiParam("选中的页数") @RequestParam("pageNum") Integer pageNum ,
                                 @ApiParam("页面显示的数据量") @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<ReservedVo> list = reservedService.selectAll();
        PageInfo<ReservedVo> pageInfo = new PageInfo<ReservedVo>(list);
        PageVO<ReservedVo> reservedVoPageVO = new PageVO<>();
        BeanUtils.copyProperties(pageInfo, reservedVoPageVO);
        reservedVoPageVO.setResult(pageInfo.getList());
        reservedVoPageVO.setPage(pageNum);
        return AjaxResult.success(reservedVoPageVO);

    }
/*
    @ApiOperation("根据对应字段查询已预订信息")
    @GetMapping("/selectByName")
    public AjaxResult selectByName( @RequestParam(value = "查询的字段") String field ,@RequestParam(value = "查询的内容")String content){

        if (field.equals("姓名")) {
            return AjaxResult.success(reservedMapper.selectByName(content));
        }
        if (field.equals("性别")) {
            return AjaxResult.success(reservedMapper.selectBySex(content));
        }
        if (field.equals("年度")) {
            return AjaxResult.success(reservedMapper.selectByYear(content));
        }
        if (field.equals("网登编号")) {
            return AjaxResult.success(reservedMapper.selectByLoginNumber(content));
        }
        if (field.equals("批次")) {
            return AjaxResult.success(reservedMapper.selectByBatch(content));
        }
        if (field.equals("年龄")) {
            return AjaxResult.success(reservedMapper.selectByAge(content));
        }
        if (field.equals("民族")) {
            return AjaxResult.success(reservedMapper.selectByNation(content));
        }
        if (field.equals("户籍类型")) {
            return AjaxResult.success(reservedMapper.selectByHouseholdType(content));
        }
        if (field.equals("学历")) {
            return AjaxResult.success(reservedMapper.selectByCultureralProgram(content));
        }
        if (field.equals("入伍批准书编号")) {
            return AjaxResult.success(reservedMapper.selectByJoinApproveId(content));
        }
        if (field.equals("入伍通知书编号")) {
            return AjaxResult.success(reservedMapper.selectByJoinInformId(content));
        }
        if (field.equals("登记报名时间")) {
            return AjaxResult.success(reservedMapper.selectByRegistrationTime(content));
        }
        return AjaxResult.error();
    }*/

    @ApiOperation("根据对应字段查询已预订信息")
    @GetMapping("/selectByFile")
    public AjaxResult selectByField(@ApiParam("查询的字段")@RequestParam(value = "field") String field ,
                                    @ApiParam("查询的内容")@RequestParam(value = "content")String content){
        return AjaxResult.success(reservedMapper.selectByFiled(field,content));
    }
    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    @ApiOperation("导入前要先下载模板")
    public AjaxResult importTemplate()
    {
        ExcelUtil<ReservedVo> util = new ExcelUtil<ReservedVo>(ReservedVo.class);
        return util.importTemplateExcel("已预订数据");
    }

    @ApiOperation("excel导入已预订信息")
    @PostMapping("/importMilitary")
    @ResponseBody
    public Result importStudent(MultipartFile file) throws Exception
    {
        return reservedService.importExcel(file);
    }
    /**
     * 导出已预订信息
     */
    @ApiOperation("导出已预订信息")
    @Log(title = "已预订信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里URLEncoder.encode可以防止中文乱码
        String fileName = "test1";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库
        List<ReservedVo> reservedVos = reservedService.selectAll();//调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), ReservedVo.class).sheet("已预订信息")
                    .doWrite(reservedVos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("按条件查询已预订信息")
    @PostMapping("/selectReserved")
    public TableDataInfo selectReserved(@RequestBody(required = false) ReservedDto reservedDto){
        startPage();
        List<ReservedVo> list = reservedService.selectReserved(reservedDto);
        return getDataTable(list);

    }

    @ApiOperation("修改是否为预定兵")
    @PutMapping("/update")
    public AjaxResult update(
            @RequestBody String[] ids,
            @RequestBody String isOrderSoldiers){
        UpdateWrapper<DoubleQualified> wrapper = new UpdateWrapper<>();
        for(String id : ids)
        {
            wrapper.eq("double_qualified_id",id).set("is_order_soldiers",isOrderSoldiers);
        }
        return toAjax(doubleQualifiedService.update(null,wrapper));
    }

}
